#This script executes event-study regressions to estimate cohort-specific 
#associations between first-generation prenatal exposure to nonattainment 
#and first-generation time use.

rm(list=ls())
gc()
library(reldist)
library(ineq)
library(stargazer)
library(data.table)
library(dplyr)
library(dtplyr)
library(ggplot2)
library(stringdist)
library(stringr)
library(lfe)
library(haven)
library(broom)
library(tidylog)
library(foreign)
library(readr)

# Set the working directory
setwd("/projects/opp_env/caa1970/")


# Source external R scripts for custom functions
source("Code/rounding.r")

# Load the ATUS dataset
load("Data/ATUS/atus_merged_data.rda")

#Figure B5

#Total Quality Time

eventB5a <- felm(quality_time ~ inst_1969+inst_1970+inst_1972+
                  inst_1973+inst_1974+inst_1975+ inst_1976 + inst_1977 + inst_1978 + inst_1979 + inst_1980|state_year+county +year_factor + month.y + survey_year_by_year + interview_day |0|county, 
                data=cps_ipums_atus_merge_pob_caa %>% filter(year.y%in%1969:1980, AGE>24 , AGE < 46, !is.na(all_fullterm)))

summary(eventB5a)


#Save the underlying point
figB5a <- eventB5a %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB5a, file="JPE_Micro/Output/B5_Figures/FigB5a.csv", row.names=F)


#Helping Children

eventB5b <- felm(quality_time_A ~ inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975+ inst_1976 + inst_1977 + inst_1978 + inst_1979 + inst_1980|state_year+county +year_factor + month.y + survey_year_by_year + interview_day |0|county, 
               data=cps_ipums_atus_merge_pob_caa %>% filter(year.y%in%1969:1980, AGE>24 , AGE < 46, !is.na(all_fullterm)))

summary(eventB5b)


#Save the underlying point
figB5b <- eventB5b %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(event_1, file="JPE_Micro/Output/B5_Figures/FigB5b.csv", row.names=F)

#Eating Meals

eventB5c <- felm(quality_time_B ~ inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975+ inst_1976 + inst_1977 + inst_1978 + inst_1979 + inst_1980|state_year+county +year_factor + month.y + survey_year_by_year + interview_day |0|county, 
               data=cps_ipums_atus_merge_pob_caa %>% filter(year.y%in%1969:1980, AGE>24 , AGE < 46, !is.na(all_fullterm)))

summary(eventB5c)


#Save the underlying point
figB5c <- eventB5c %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB5c, file="JPE_Micro/Output/B5_Figures/FigB5c.csv", row.names=F)

#Arts and Sports

eventB5d <- felm(quality_time_C ~ inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975+ inst_1976 + inst_1977 + inst_1978 + inst_1979 + inst_1980|state_year+county +year_factor + month.y + survey_year_by_year + interview_day |0|county, 
               data=cps_ipums_atus_merge_pob_caa %>% filter(year.y%in%1969:1980, AGE>24 , AGE < 46, !is.na(all_fullterm)))

summary(eventB5d)


#Save the underlying point
figB5d <- eventB5d %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB5d, file="JPE_Micro/Output/B5_Figures/FigB5d.csv", row.names=F)

#Child Care
eventB5e <- felm(quality_time_D ~ inst_1969+inst_1970+inst_1972+
                 inst_1973+inst_1974+inst_1975+ inst_1976 + inst_1977 + inst_1978 + inst_1979 + inst_1980|state_year+county +year_factor + month.y + survey_year_by_year + interview_day |0|county, 
               data=cps_ipums_atus_merge_pob_caa %>% filter(year.y%in%1969:1980, AGE>24 , AGE < 46, !is.na(all_fullterm)))

summary(eventB5e)

#Save the underlying point
figB5e <- eventB5e %>% tidy() %>% mutate_each(funs(signif(.,4)),-term)  %>% 
  filter(grepl("inst", term), !is.na(estimate))%>%
  mutate(year=as.numeric(gsub("inst_","", gsub("_bin", "", term)))) %>% 
  bind_rows(., data.frame(estimate=0, std.error=0, year=1971)) %>% filter(year<=1975) 
write.csv(figB5e, file="JPE_Micro/Output/B5_Figures/FigB5e.csv", row.names=F)